package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.PatrolDao;
import com.hdaccp.xingmu.entity.Patrol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/patrol")
public class PatrolCon {

    @Autowired
    private PatrolDao patrolDao;

    @PersistenceContext
    private EntityManager entityManager ;
    //跳转
    @RequestMapping("Qtiaozhuan")
    public String Qtianzhuan(){
        return "WEB-INF/jsp/shequfuwu/baoanxunluo";
    }
    //查询 条件查询
    @RequestMapping("/chaxunquanbu")
    @ResponseBody
    public Map<String ,Object> quanbujinayi(String firstDate,String secondDate,int pageSize,int pageNumber,String type) {

        String jpal="SELECT p.id,p.order,p.name,i.name as iname,p.cname,p.date,p.results,p.text FROM Patrol AS p \n" +
                "INNER JOIN Partoltype AS i ON p.type = i.id WHERE 1=1";

       if (firstDate!=null && !firstDate.equals("") && secondDate!=null && !secondDate.equals("") ){
            jpal=jpal+" and date>=" +"'" + secondDate +"' and date<=" +"'" + firstDate +"' ";
        }else if (type!=null && !type.equals("")){
           jpal=jpal+" and type=" + "'" + type +"'";
       }


        Query qu = entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((pageNumber - 1) * pageSize);
        //结束页数
        qu.setMaxResults(pageSize);

        //查询多少条数据
        String jpa = "select count(m) from Patrol m where 1=1";

        if (firstDate!=null && !firstDate.equals("") && secondDate!=null && !secondDate.equals("") ){
            jpa=jpa+" and date>=" +"'" + secondDate +"' and date<=" +"'" + firstDate +"' ";
        }else if (type!=null && !type.equals("")){
            jpa=jpa+" and type=" + "'" + type +"'";
        }

        Query q = entityManager.createQuery(jpa);

        Long count = (Long) q.getSingleResult();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", qu.getResultList());
        return map;
    }

    //添加建议信息
    @RequestMapping("/Qtianjia")
    @ResponseBody
    public Map<String ,Object> add2(Patrol patrol,int pageSize,int pageNumber){
        Patrol patrol1=new Patrol();

        patrol1.setOrder(patrol.getOrder());
        patrol1.setCname(patrol.getCname());
        patrol1.setDate(patrol.getDate());
        patrol1.setResults(patrol.getResults());
        patrol1.setText(patrol.getText());
        patrol1.setName(patrol.getName());
        patrol1.setType(patrol.getType());

        patrolDao.save(patrol1);
        return quanbujinayi(null,null,pageSize,pageNumber,null);
    }


    //修改数据
    @RequestMapping("Qupd")
    @ResponseBody
    public Map<String ,Object> Qupd(int id,String order,String name,long type,String cname,java.sql.Date date,String results,String text,int pageSize,int pageNumber){
        Patrol patrol=new Patrol();
        patrol.setId(id);
        patrol.setOrder(order);
        patrol.setName(name);
        patrol.setType(type);
        patrol.setCname(cname);
        patrol.setDate(date);
        patrol.setResults(results);
        patrol.setText(text);

        Patrol advice2 = patrolDao.save(patrol);

        return quanbujinayi(null,null,pageSize,pageNumber,null);

    }




    //删除数据
    @RequestMapping("/Qdel")
    @ResponseBody
    public Map<String ,Object> Qdel(long id,int pageNumber,int pageSize){

        patrolDao.deleteById(id);


        return quanbujinayi(null,null,pageSize,pageNumber,null);
    }


}
